package com.isoft.illustrationsdraw.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.isoft.illustrationsdraw.service.IllustrationDTOService;
import com.isoft.illustrationsdraw.service.IllustrationService;
import com.isoft.illustrationsdraw.service.JWTService;
import com.isoft.illustrationsdraw.Constants;
import com.isoft.illustrationsdraw.dto.IllustrationDetailDTO;
import com.isoft.illustrationsdraw.entity.Illustration;
import com.isoft.illustrationsdraw.entity.LikeIllustration;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@RestController
@RequestMapping("/api/illustration")
public class IllustrationController {

    private final IllustrationDTOService illustrationDTOService;
    private final IllustrationService illustrationService;
    private final JWTService jwtService;

    public IllustrationController(IllustrationDTOService illustrationDTOService, IllustrationService illustrationService, JWTService jwtService) {
        this.illustrationDTOService = illustrationDTOService;
        this.jwtService = jwtService;
        this.illustrationService = illustrationService;
    }

    
    @GetMapping("/detail/{id}")
    public ResponseEntity<?> getIllustrationDetail(@RequestParam String token, @PathVariable String id) {
        
        try {
            Long illustrationId = Long.valueOf(id);
    
            IllustrationDetailDTO illustrationDetailDTO = illustrationDTOService.attemptGetIllustrationDetailDTO(illustrationId, token);
            if (illustrationDetailDTO == null) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Illustration not found.");
            }
            return ResponseEntity.ok(illustrationDetailDTO);
        } catch (NumberFormatException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid illustration ID format.");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred while fetching the illustration details.");
        }
    }
    

    // 改变作品信息
    @PostMapping("/changeinfo")
    public ResponseEntity<?> changeDetail(@RequestParam String token, @RequestBody Illustration illustration) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        if (illustrationService.updateById(illustration)) {
            return ResponseEntity.ok("Your change has been successful!");
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
        .body("Error message or stack trace here");
    }

    @PostMapping("/addlike")
    public ResponseEntity<?> addLikeIllustration(@RequestParam String token, @RequestBody LikeIllustration likeIllustration) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        Long userId = jwtService.getUserIdFromToken(token);
        likeIllustration.setUserId(userId);
        if (illustrationDTOService.addLikeIllustration(likeIllustration)) {
            return ResponseEntity.ok("Like successfully!");
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
        .body("Error message or stack trace here");
    }

    @PostMapping("/removelike")
    public ResponseEntity<?> removeLikeIllustration(@RequestParam String token, @RequestBody LikeIllustration likeIllustration) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        Long userId = jwtService.getUserIdFromToken(token);
        likeIllustration.setUserId(userId);
        if (illustrationDTOService.removeLikeIllustration(likeIllustration)) {
            return ResponseEntity.ok("Remove like successfully!");
        }

        return ResponseEntity.badRequest().body("Invalid remove request");
    }

    @GetMapping("/randomIllustration")
    public ResponseEntity<?> getRandomIllustration(@RequestParam String token) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        return ResponseEntity.ok(illustrationService.getRandomIllustration(1, 1).get(0));
    }

    @GetMapping("/randomTag")
    public ResponseEntity<?> getRandomTag(@RequestParam String token) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        Long userId = jwtService.getUserIdFromToken(token);
        return ResponseEntity.ok(illustrationDTOService.getRecommandTag(userId));
    }

    @GetMapping("/get-tag-work")
    public ResponseEntity<?> getTagAndWork(@RequestParam String token, @RequestParam Long tagId, @RequestParam Integer page) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        Long userId = jwtService.getUserIdFromToken(token);
        
        return ResponseEntity.ok(illustrationDTOService.getTagAndWork(userId, tagId, page));
    }

    @GetMapping("/init-tag-work")
    public ResponseEntity<?> getTagAndWork(@RequestParam String token) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        Long userId = jwtService.getUserIdFromToken(token);
        
        return ResponseEntity.ok(illustrationDTOService.initTagAndWork(userId));
    }

    @GetMapping("/get-lastest-work")
    public ResponseEntity<?> getLastestWork(@RequestParam String token, @RequestParam Integer page) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        Long userId = jwtService.getUserIdFromToken(token);
        return ResponseEntity.ok(illustrationDTOService.getLastestWork(userId, page));
    }


                       
}
